package com.central.apps.handler.asset;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.ToolUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理资产处置业务逻辑
 *
 * @author lidachao
 * @date 2022/7/28
 */
@Component
public class AssetDisposalHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        // 1.获取参数数据
        String disposalnum = MapUtils.getString(data, "disposalnum");
        // 2.获取资产处置资产净值、资产数量
        String searchSql ="select assetvalue from gq_assetdisposaldetail where disposalnum ='"+disposalnum+"';";
        List<Map<String, Object>> resultList = handlerService.findBySql(searchSql);

        if (CollectionUtils.isEmpty(resultList)) {
            return;
        }

        // 3.计算资产处置资产净值
        Map<String, Object> map = new HashMap<String, Object>();
        float assetvalue = 0f;
        for (int i = 0; i < resultList.size(); i++) {
            map = resultList.get(i);
            assetvalue = (float) map.get("assetvalue") + assetvalue;
        }
        // 4.计算资产处置资产数量
        float quantity = (float) resultList.size();

        // 5.更新资产处置资产净值、资产数量信息
        String updSql = "update gq_assetdisposalhead set quantity ='" + quantity + "'," + "currentvalue= " + assetvalue
                + " where disposalnum ='" + disposalnum + "';";
        handlerService.updateBySql(updSql);
    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if (flowNode.getId().equals("submit")) {
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_assetdisposalhead where id ="+id);
            String disposalnum =MapUtils.getString(bySql.get(0),"disposalnum");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_assetdisposaldetail where disposalnum ='"+disposalnum+"'");
            if(ToolUtil.isEmpty(bySql1)){
                return "子表数据为空,请添加数据后在提交流程";
            }
        }
        return null;
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("approval")){
            boolean flag =op.isFlag();
            if(flag){
                //获取主表编号,处置类型
                List<Map<String, Object>> bySql = handlerService.findBySql("select disposalnum,disposaltype from gq_assetdisposalhead where id =" + id);
                String disposalnum =MapUtils.getString(bySql.get(0),"disposalnum");
                String disposaltype =MapUtils.getString(bySql.get(0),"disposaltype");

                if (disposaltype.equals("转移")) {
                    disposaltype = "闲置";
                }

                //根据主表编号查询子表
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select assetnum from gq_assetdisposaldetail where disposalnum ='"+disposalnum+"'");
                if(ToolUtil.isNotEmpty(bySql1)){
                    for(Map<String, Object> map :bySql1){
                        String assetnum =MapUtils.getString(map,"assetnum");
                        //更新对应资产编号的状态
                        handlerService.updateBySql("update gq_assethead set status ='"+disposaltype+"' where assetnum ='"+assetnum+"'");
                    }
                }
            }
        }
    }
}
